#include "debug.h"

Debug debug;

bool Debug::existFlag(Debug::debug_t d)
{
	return debug_features__.find(d) != debug_features__.end();
}

void Debug::createFlag(Debug::debug_t d, bool enabled)
{
	Debug::debug_feature_t dbg;
	dbg.enabled = enabled;
	dbg.outstream = &dummy_out__;

	debug_features__[d] = dbg;
}

Debug::Debug()
{
}

Debug::~Debug()
{
}

void Debug::setFlag(Debug::debug_t d)
{
	if (!existFlag(d))
		createFlag(d, true);
	else
		debug_features__[d].enabled = true;
}

void Debug::unsetFlag(Debug::debug_t d)
{
	if (!existFlag(d))
		createFlag(d, false);
	else
		debug_features__[d].enabled = false;
}

void Debug::toggleFlag(Debug::debug_t d)
{
	if (!existFlag(d))
		createFlag(d, false);
	else
		debug_features__[d].enabled = !debug_features__[d].enabled;
}

void Debug::setFlagStream(Debug::debug_t d, std::ostream* os)
{
	if (!existFlag(d))
		createFlag(d, false);
	if (os == 0)
		debug_features__[d].outstream = &dummy_out__;
	else
		debug_features__[d].outstream = os;
}

bool Debug::operator[](Debug::debug_t d)
{
	if (!existFlag(d))
		return false;
	else
		return debug_features__[d].enabled;
}

std::ostream& Debug::out(Debug::debug_t d)
{
	if (!existFlag(d))
		return dummy_out__;
	else
		return *debug_features__[d].outstream;
}

